﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Xpo;
using Component;

namespace Component
{
    public class ProductDB : Base
    {
     
        /// <summary>
        /// public static XPCollection<Product> GetAll()
        /// </summary>
        /// <returns>XPCollection<Product></returns>
        public static XPCollection<Product> GetAll(bool admin)
        {
               XPCollection<Product> _xpProduct = new XPCollection<Product>(KetNoiCSDL.session);
            _xpProduct.Filter = FilterAll(admin);
            return _xpProduct;
        }
        /// <summary>
        /// public static Product GetObjectByid(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Product GetObjectByid(int id, bool admin)
        {
            XPCollection<Product> _xpProduct = new XPCollection<Product>(KetNoiCSDL.session);
            _xpProduct.Filter = FilterById(id, true);
            if (_xpProduct.Count > 0)
            {
                return _xpProduct[0];
            }
            return new Product (KetNoiCSDL.session);
        }
       
        /// <summary>
        /// private static Category GetCategoryByidCategory(int idCategory,bool admin)
        /// </summary>
        /// <param name="idCategory"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public static XPCollection<Product> GetAllObjByIdCatSub(int id_SubCat, bool admin)
        {
            XPCollection<Product> _xpProduct = new XPCollection<Product>(KetNoiCSDL.session);
            if (admin == false)
                _xpProduct.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumProduct._Active + ConstString._dauBangVaAnd + ColumProduct._SubCat + "." + ColumCategory._Id + ConstString._dauBang, true, id_SubCat);
            else
                _xpProduct.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumProduct._SubCat + "." + ColumCategory._Id + ConstString._dauBang, id_SubCat);
            return _xpProduct;
        }
        /// <summary>
        ///       public static XPCollection<Product> GetAllObjByIdCat(int idCat, bool admin)
        /// </summary>
        /// <param name="idCat"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public static XPCollection<Product> GetAllObjByIdCat(int idCat, bool admin)
        {
            XPCollection<Product> _xpProduct = new XPCollection<Product>(KetNoiCSDL.session);
            if (admin == false)
                _xpProduct.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumProduct._Active + ConstString._dauBangVaAnd + ColumProduct._SubCat +"."+ColumCategory._Id+ ConstString._dauBang, true, idCat);
            else
                _xpProduct.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumProduct._SubCat + "." + ColumCategory._Id+ ConstString._dauBang, idCat);
            return _xpProduct;
        }
        /// <summary>
        ///    public static XPCollection<Product> LayTatCaCacSPCungLoai(int idProduct)
        /// </summary>
        /// <param name="idProduct"></param>
        /// <returns></returns>
        public static XPCollection<Product> LayTatCaCacSPCungLoai(int idProduct)
        {
            try
            {
                return GetAllObjByIdCatSub(GetObjectByid(idProduct, false).SubCat.Id, false);
            }
            catch { return null; }
        }
        /// <summary>
        ///    public static XPCollection<Product> GetAllSpecialProduct(bool admin)
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        
        public static XPCollection<Product> GetAllSpecialProduct(bool admin)
        {
            XPCollection<Product> _xpProduct = new XPCollection<Product>(KetNoiCSDL.session);
            if (admin == false)
                _xpProduct.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumProduct._Active + ConstString._dauBangVaAnd + ColumProduct._Special + ConstString._dauBang, true,true);
            else
                _xpProduct.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumProduct._Special + "=" + ConstString._dauBang,true);
            return _xpProduct;
        }
        /// <summary>
        ///    public static string GetContentByLanguage(string columName, Product pro)
        /// </summary>
        /// <param name="columName"></param>
        /// <param name="pro"></param>
        /// <returns></returns>
        public static string GetContentByLanguage(string columName, Product pro)
        {
            switch (columName)
            {
                case ColumProduct._Name:
                    {
                        if (LanguageVN == true)
                            return pro.Name;
                        return pro.NameEL;
                    }
                case ColumProduct._Description:
                    {
                        if (LanguageVN == true)
                            return pro.Description;
                        return pro.DescriptionEL;
                    }
                case ColumProduct._VND:
                    {
                        if (LanguageVN == true)
                            return pro.VND.ToString();
                        return pro.USD.ToString();
                    }
            }
            return "";
        }
        /// <summary>
        ///     public static XPCollection<Product> GetObjectByName(string namePro,bool admin)
        /// </summary>
        /// <param name="namePro"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public static XPCollection<Product> GetObjectByName(string namePro,bool admin)
        {
            XPCollection<Product> _xpProduct = new XPCollection<Product>(KetNoiCSDL.session);
            _xpProduct.Filter = FilterByString(ColumProduct._Name, namePro, admin);
            return _xpProduct;
        }
        /// <summary>
        /// 26-06
        /// Xóa  hết các product của sub category
        /// </summary>
        /// <param name="idSubCat"></param>
        public static void DeleteAllProductOfSubCat(int idSubCat)
        {
            XPCollection<Product> xpProduct = new XPCollection<Product>(KetNoiCSDL.session);
            xpProduct = ProductDB.GetAllObjByIdCatSub(idSubCat, true);
            while (xpProduct.Count > 0)
            {
                ProductDB.Delete(xpProduct[0]);
            }
        }
    }
}

